#version 420

struct Light {
    vec4 pos;        // Light position in eye coords.
    vec3 ambient;    // Ambient light intensity
    vec3 diffuse;    // Diffuse light intensity
    vec3 specular;   // Specular light intensity
};

struct Material {
    vec3 ka;         // Ambient reflectivity
    vec3 kd;         // Diffuse reflectivity
    vec3 ks;         // Specular reflectivity
    float shininess; // Specular shininess factor
};

struct Vertex {
    vec3 pos;
    vec3 normal;
    Material material;
};


vec3 phong(in vec3 antiViewDirection/*opposite view direction*/, in Light light, in Vertex vertex)
{
    vec3 antiLightDirection;    // opposite incident light direction


    if(0.0 == light.pos.w)  //  directional light
    {
        antiLightDirection = normalize(-vec3(light.pos));
    }
    else
    {
        antiLightDirection = normalize(light.pos.xyz - vertex.pos);
    }

    vec3 ambient = light.ambient * vertex.material.ka;
    vec3 diffuse = light.diffuse * vertex.material.kd * dot(antiLightDirection, vertex.normal);

    vec3 reflectLight = reflect(antiLightDirection, vertex.normal);
    float kReflect = pow(dot(reflectLight, antiViewDirection), vertex.material.shininess);
    /*
    vec3 hv = antiLightDirection + antiViewDirection;
    float kReflect = pow(dot(hv, vertex.normal), vertex.material.shininess);
    */
    vec3 specular = light.specular * vertex.material.ks * kReflect;

    return clamp(ambient + diffuse + specular, vec3(0.0), vec3(1.0));
}

